package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AddRoleDto;
import com.zzyl.dto.RolePageQueryDto;
import com.zzyl.dto.UpdateRoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("角色分页查询")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<Role>>  page(@PathVariable("pageNum")Integer pageNum,
                                                    @PathVariable("pageSize")Integer pageSize,
                                                    @RequestBody RolePageQueryDto dto){
        PageResponse<Role> page = roleService.page(pageNum, pageSize, dto);
        return ResponseResult.success(page);
    }
    @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult add(@RequestBody AddRoleDto dto){
        roleService.add(dto);
        return ResponseResult.success();
    }

    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResources (@PathVariable("roleId")Long roleId){
        List<String> list = roleService.findCheckedResources(roleId);
        return ResponseResult.success(list);
    }
    @ApiOperation("角色修改")
    @PatchMapping
    public ResponseResult update(@RequestBody UpdateRoleDto dto){
        roleService.update(dto);
        return ResponseResult.success();
    }

    @ApiOperation("角色删除")
    @DeleteMapping("/{roleIds}")
    public ResponseResult delete(@PathVariable("roleIds") Long roleId){
        roleService.deleteRoleById(roleId);
        return ResponseResult.success();
    }

    @ApiOperation("用户下拉框")
    @PostMapping("/init-roles")
    public ResponseResult<List<Role>> list(){
        List<Role> roles = roleService.list();
        return ResponseResult.success(roles);
    }
}
